import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { NativeService } from '../../../providers/NativeService';
import html2canvas from 'html2canvas';

@IonicPage()
@Component({
  selector: 'page-share-demo',
  templateUrl: 'share-demo.html',
})
export class ShareDemoPage {
  text = 'https://yanxiaojun617.com/fileService/file/test/png.png';

  shareing: boolean = false;
  pagebase64: string = null;

  constructor(public navCtrl: NavController, public navParams: NavParams, public nativeService: NativeService) {
  }

  shareText() {
    this.nativeService.share('哈哈');
  }

  shareImageUrl() {
    this.nativeService.share(null, 'https://yanxiaojun617.com/fileService/file/test/png.png');
  }

  shareImageBase64() {
    this.nativeService.share(null, '');
  }

  shareDom() {
    if (this.pagebase64) {
      this.nativeService.share(null, this.pagebase64);
      return;
    }
    this.shareing = true;
    html2canvas(document.querySelector('.code-wrap'), {useCORS: true}).then(canvas => {
      this.pagebase64 = canvas.toDataURL('image/png');
      this.nativeService.share(null, this.pagebase64);
      this.shareing = false;
    }).catch(() => {
      this.shareing = false;
    });
  }

}
